﻿#pragma once


#include <QDialog>
#include <QPushButton>
#include "QListView"
#include "QStringListModel"
#include "QFileDialog"
#include "QMessageBox"
#include "QApplication"
#include "QVBoxLayout"
#include <QDesktopWidget>
#include <QLibrary>
#include <QApplication>
#include <QLabel>
#include <QComboBox>
#include "QListView"
#include "QStringListModel"
#include "QLineEdit"
#include "QSpinBox"
#include "QRadioButton"
#include <QSettings>

#include "commonfunction.h"

#include "halcon_opencv.h"

using namespace std;

#include "qt_graphics_view.h"
#include "qt_graphics_shape_rectangle.h"

#include <QSettings>
#include "qwidget_dl_para1.h"
#include "qwidget_dl_para2.h"
//#include "function_dl_3.h"
#include <QSettings>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/fmt/ostr.h"
extern std::shared_ptr<spdlog::logger> logger;
extern void initHalconGpu();


struct HalconAIResult
{
	double threshold_score;
	HObject image;
	string  name;
	float area = 0;
	float x;
	float y;
};

struct DefectStandard
{
    string name="";
    float threshold_score=0;
    float threshold_size=0;
};


struct DetectionStandard
{
    string name="";
    float threshold_score=0;
    float threshold_size_min=0;
    float threshold_size_max = 0;
    int   threshold_number =1;
};

class FunctionDL:public QDialog
{
public:
	vector<GraphicsShapeRectangleCirce*> m_vector_graphics_shape_rectangle;
	vector<GraphicsShapeRectangleCirce*> m_vector_rectangle_ear;
	int m_camera_index = 0;
	int m_detection_index = 0;
	QSettings* m_qsettings;
	QString m_section = "";
	QString m_save_image_dir;
	
	

	QtQGraphicsView* m_view;

    QPushButton* m_pushbutton_open_model= nullptr;
    QLabel     * m_qlabel_model_path= nullptr;
	QString RetrainedModelFileName = "";

	QPushButton* m_pushbutton_preprocess;
	QLabel     * m_qlabel_preprocess_path;
	QString PreprocessFileName = "";



    QPushButton* m_pushbutton_show_orign_image= nullptr;
    QPushButton* m_pushbutton_detection= nullptr;
    QPushButton* m_pushbutton_save_parameter= nullptr;
    QPushButton* m_pushbutton_return= nullptr;

    QLabel* m_qlabel_message= nullptr;

	int m_result;

	int row1, column1, row2, column2;
	int ret, message;

	int m_size_error_now;

	Mat m_image;

	Mat m_show_image;

	QWidgetDlPara1* m_qwidget_dl_para1 = nullptr;


	vector<HalconAIResult> result_defect;
	vector<DefectStandard> m_vector_defect_standard;

//    QComboBox* m_qcombox_unuse_use_ear = nullptr;
//	int m_unuse_use_ear;
//	QWidgetDlPara2* m_qwidget_dl_para2 = nullptr;
//	vector<HalconAIResult> result_ear;
//	vector<DetectionStandard> m_vector_ear_standard;

    
    HObject m_region_segment;

	bool m_state = false;
    HTuple DLModelHandle,DLPreprocessParam;

public:
    explicit FunctionDL(QDialog *parent = nullptr, QSettings* qsettings = nullptr, QString section = "", int camera_index = 0, int detection_index = 0);

    ~FunctionDL() ;
	bool         loadmodel();
    virtual void detectionImage();
    virtual bool detection(HObject image_in,QString& message);
    virtual void draw( Mat image_in,Mat& image_out);
    virtual void loadParameter();
    virtual void saveParameter();
    virtual bool showDialog();
	void save_dl_prar1(QWidgetDlPara1* roi_sqllite, vector<DefectStandard>& vector_defect_standard);
//	void save_dl_prar2(QWidgetDlPara2* roi_sqllite, vector<DetectionStandard>& vector_defect_standard);
};
#pragma execution_character_set("utf-8")
